package com.jiandan.meizi;

import java.util.LinkedList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.jiandan.load.ListAdpter;
import com.jiandan.utils.HtmlParser;
import com.jiandan.utils.SimpleHttpClient;
import com.jiandan.utils.Tools;

/**
 * http://jandan.net/ooxx
 * 
 * @author mr.HE
 * 
 */
public class MeiZiActivity extends Activity implements OnClickListener {

    private ListView listview;
    private String NextPage = "http://jiandan.net/ooxx";
    private LinkedList<String> imgUrlList = null;
    private View footView;
    private Button nextBtn;
    private ProgressBar pb;
    private ListAdpter adapter;
    LinkedList<String> nextList;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @SuppressWarnings("unchecked")
        @Override
        public void handleMessage(Message msg) {
            switch (msg.arg1) {
                case -1:
                    Tools.progressDialogDissmiss();
                    Toast.makeText(MeiZiActivity.this, "解析图片地址失败", Toast.LENGTH_LONG).show();
                    break;
                case 1:
                    NextPage = (String) ((Object[]) msg.obj)[0];
                    if (imgUrlList == null) {
                        imgUrlList = (LinkedList<String>) ((Object[]) msg.obj)[1];
                        adapter = new ListAdpter(MeiZiActivity.this, imgUrlList);
                        listview.setAdapter(adapter);
                        Tools.progressDialogDissmiss();
                    } else {
                        int m = imgUrlList.size();
                        nextList = (LinkedList<String>) ((Object[]) msg.obj)[1];
                        for (int i = 0; i < nextList.size(); i++) {
                            imgUrlList.add(nextList.get(i));
                        }
                        if (imgUrlList.size() == m + nextList.size()) {
                            nextList.clear();
                        }
                        adapter.notifyDataSetChanged();
//                        nextBtn.setVisibility(View.VISIBLE);
                        pb.setVisibility(View.GONE);
                        Tools.progressDialogDissmiss();
                    }
                    break;
                case 0:
                    Tools.buildLoadingDialog(MeiZiActivity.this);
                    new NextPageThread(NextPage).start();
                    break;
                case -2:
                    new NextPageThread(NextPage).start();
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.meizi);

        if (Tools.isConnect(this)) {
            Toast.makeText(this, "请检查网络", Toast.LENGTH_LONG).show();
            return;
        }

        listview = (ListView) findViewById(R.id.listview);
        footView = getLayoutInflater().inflate(R.layout.next_page, null);
        nextBtn = (Button) footView.findViewById(R.id.bt_load);
        nextBtn.setOnClickListener(this);
        pb = (ProgressBar) footView.findViewById(R.id.pg);
        listview.addFooterView(footView);
//        listview.setOnItemClickListener(new OnItemClickListener() {
//
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                if (imgUrlList != null) {
//                    Intent intent = new Intent(MeiZiActivity.this, ViewImgActivity.class);
//                    intent.putExtra("imgUrl", imgUrlList.get(position));
//                    startActivity(intent);
//                }
//
//            }
//        });
        Tools.buildLoadingDialog(this);
        new NextPageThread(NextPage).start();
    }

    class NextPageThread extends Thread {
        private String Url;

        public NextPageThread(String url) {
            super();
            Url = url;
        }

        @Override
        public void run() {

            try {
                HtmlParser parse = new HtmlParser();
                SimpleHttpClient client = new SimpleHttpClient();
                parse.setHtml(client.get(Url));
                Message msg = new Message();
                Object[] obj = new Object[2];
                obj[0] = parse.getPageNavi();
                obj[1] = parse.handleImgs();
                msg.obj = obj;
                if (msg.obj == null) {
                    msg.arg1 = -1;
                } else {
                    msg.arg1 = 1;
                }
                handler.sendMessage(msg);
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("ParseHtmlError", "获取html标签出错，将重新调用线程加载下一页");
                Message msg = new Message();
                msg.arg1 = -2;
                handler.sendMessage(msg);
            }
        }

    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.bt_load) {
//            nextBtn.setVisibility(View.GONE);
            pb.setVisibility(View.VISIBLE);
            Tools.buildLoadingDialog(this);
            new NextPageThread(NextPage).start();
        }

    }

}
